#include "temp_class.hxx"
#include "stdio.h"
#include <iostream>

template<class T> A<T>::A() {
    printf("This is a constructure function...\n");
}

template<class T> T A<T>::g(T a, T b) {
    printf("This is a another function...\n");
    return a + b;
}    

template<class T, int MAXSIZE> 
Stack<T, MAXSIZE>::Stack():numElems(0) {
}

template<class T, int MAXSIZE>
void Stack<T, MAXSIZE>::push(T const& elem) {
    if(numElems == MAXSIZE) {
        throw std::out_of_range("Stack<>::push(): stack is full");
    }
    elems[numElems] = elem;
    ++numElems;
}

template<class T,int MAXSIZE>
void Stack<T,MAXSIZE>::pop(){
    if (numElems <= 0) {
        throw std::out_of_range("Stack<>::pop(): empty stack");
    }
    --numElems;               // 减少元素的个数
}

template <class T,int MAXSIZE>
T Stack<T,MAXSIZE>::top()const{
    if (numElems <= 0) {
        throw std::out_of_range("Stack<>::top(): empty stack");
    }
    return elems[numElems-1];  // 返回最后一个元素
}

template<typename T> 
int CompareDemo<T>::compare(const T& a,const T& b){
   if((a-b)>0)
       return 1;
   else if((a-b)<0)
       return -1;
   else
       return 0;
}

int main() {
    A<int> a;
    printf("a(2, 3) a.g is %d\n", a.g(2, 3));

    try {
       Stack<int,20>  int20Stack;  // 可以存储20个int元素的栈
        Stack<int,40>  int40Stack;  // 可以存储40个int元素的栈
        Stack<std::string,40> stringStack; // 可存储40个string元素的栈
    
        // 使用可存储20个int元素的栈
        int20Stack.push(7);
        std::cout << int20Stack.top() << std::endl;    //7
        int20Stack.pop();
    
        // 使用可存储40个string的栈
        stringStack.push("hello");
        std::cout << stringStack.top() << std::endl;    //hello
        stringStack.pop();    
        stringStack.pop();    //Exception: Stack<>::pop<>: empty stack
    }
    catch (std::exception const& ex) {
        std::cerr << "Exception: " << ex.what() << std::endl;
//        return EXIT_FAILURE;  // 退出程序且有ERROR标记
    }

    CompareDemo<int> cd;
    std::cout<<cd.compare(2, 3)<<std::endl;
    return 0;

}
